package com.coscon.service.impl;

import com.coscon.service.OshiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class OshiServiceImpl implements OshiService {

    private SystemInfo systemInfo = new SystemInfo();


    @Override
    public Map<String, String> getSystemMemoryInfo() {
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        //总内存
        long totalByte = memory.getTotal();
        //剩余
        long acaliableByte = memory.getAvailable();

        //总大小
        String memoryTotalSize = FileUtils.byteCountToDisplaySize(totalByte);
        //已使用
        String memoryUser = FileUtils.byteCountToDisplaySize(totalByte-acaliableByte);
        //剩余可用
        String memoryAcaliable = FileUtils.byteCountToDisplaySize(acaliableByte);
        //使用率
        String memoryUsageRate = new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte);

        Map<String,String> map =  new HashMap<>(4);
        map.put("memoryTotalSize",memoryTotalSize);
        map.put("memoryUser",memoryUser);
        map.put("memoryAcaliable",memoryAcaliable);
        map.put("memoryUsageRate",memoryUsageRate);

        return map;
    }
}
